package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Comarticle;
import com.rk.admin.service.ComarticleService;
import com.rk.admin.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Api(tags = "合作交流")
@RestController
@CrossOrigin //前后端分离配置跨域访问
@RequestMapping("/Comarticle")
public class ComarticleController {

        @Autowired
        private ComarticleService comarticleService;


        @ApiOperation(("分页查询合作交流"))
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page",value = "分页起始页码",required = true,example = "1"),
                @ApiImplicitParam(name = "limit",value = "每页数据条数",required = true,example = "5"),
                @ApiImplicitParam(name = "title",value = "文章标题",example = "榆林市"),
        })
        @GetMapping("/findPage")
        public CommonResult findPageBen(@RequestParam(required = true) Integer page,
                                        @RequestParam(required = true) Integer limit,
                                        @RequestParam(required = false) String title)
        {
            return CommonResult.success(comarticleService.selectPage(page,limit,title));
        }


        /**
         * 通过id删除
         * @param id
         * @return
         */
        @SystemControllerLog(description ="删除合作交流文章")
        @ApiOperation("删除文章")
        @PostMapping("/deletebyid")
        public CommonResult deletebyid(Integer id){
            Boolean flag = comarticleService.deleteById(id);
            if(flag){//删除成功
                return CommonResult.success(flag);
            }else {
                return CommonResult.failed("删除失败!");
            }
        }


        /**
         * 通过id查询文章信息
         */
        @ApiOperation("通过id查询文章")
        @GetMapping("/findbyid")
        public CommonResult findbyid(@RequestParam(value = "id",required = true) Integer id){
            List<Comarticle> list=new ArrayList<>();
            list.add(comarticleService.findbyId(id));
            return  CommonResult.success(list);
        }


        /**
         * 修改文章
         */
        @SystemControllerLog(description ="修改合作交流文章")
        @ApiOperation("通过id修改文章")
        @PostMapping("/updatebyid")
        public CommonResult updatebyid(@RequestBody Comarticle comarticle){
            //设置修改时间
            comarticle.setEdittime(new Date());
            Boolean flag = comarticleService.updatebyId(comarticle);

            if(flag){//如果修改成功
                return CommonResult.success(flag);
            }else {
                return CommonResult.failed("修改失败!");
            }
        }
}
